﻿using System;
using System.Linq;
using txcloud.sdk.Core;
using TencentCloud.Lighthouse.V20200324.Models;
using TencentCloud.Lighthouse.V20200324;

namespace txcloud.sdk.Lighthouse
{
    public class Lighthouse
    {
        internal TxCred cred { get; set; }
        internal TxProfile profile { get; set; }

        internal TxRegion Region { get; set; }
        internal string InstanceId { get; set; }
        public Lighthouse()
        {
            this.cred = TxCloudSDK.TxCred;
            profile = new TxProfile("lighthouse.tencentcloudapi.com");
            Region= new TxRegion(TxCloudSDK.Config.Text("地区"));
            InstanceId= TxCloudSDK.Config.Text("实例ID");
        }
        public Lighthouse(TxCred cred, string region, string instanceId)
        {
            this.cred = cred;
            profile = new TxProfile("lighthouse.tencentcloudapi.com");
            Region = new TxRegion(region);
            InstanceId = instanceId;
        }
        //https://console.cloud.tencent.cn/api/explorer?Product=lighthouse&Version=2020-03-24&Action=DescribeFirewallRules
        public Firewall ReadFirewall()
        {
            // 实例化要请求产品的client对象,clientProfile是可选的
            LighthouseClient client = new LighthouseClient(cred.Get(), Region.Get(), profile.Get());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeFirewallRulesRequest req = new DescribeFirewallRulesRequest();
            req.InstanceId = InstanceId;
            req.Offset = 0;
            req.Limit = 100;
            // 返回的resp是一个DescribeFirewallRulesResponse的实例，与请求对象对应
            DescribeFirewallRulesResponse resp = client.DescribeFirewallRulesSync(req);
            return new Firewall(resp);
        }

        public bool UpdateFirewall(Firewall firewall)
        {
            LighthouseClient client = new LighthouseClient(cred.Get(), Region.Get(), profile.Get());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            ModifyFirewallRulesRequest req = new ModifyFirewallRulesRequest();
            req.InstanceId = InstanceId;
            req.FirewallRules = firewall.Rules.ToArray();
            if (firewall.FirewallVersion != null) req.FirewallVersion = firewall.FirewallVersion;
            // 返回的resp是一个ModifyFirewallRulesResponse的实例，与请求对象对应
            ModifyFirewallRulesResponse resp = client.ModifyFirewallRulesSync(req);

            return !string.IsNullOrEmpty(resp.RequestId);
        }
        public string ReadSystemState()
        {
            LighthouseClient client = new LighthouseClient(cred.Get(), Region.Get(), profile.Get());
            DescribeInstancesRequest req = new DescribeInstancesRequest();
            req.InstanceIds = new string[] {InstanceId};
            DescribeInstancesResponse resp = client.DescribeInstancesSync(req);

            if (resp.InstanceSet != null && resp.InstanceSet.Length > 0) return resp.InstanceSet[0].InstanceState;
            return string.Empty;
        }
        public bool Rebot()
        {
            LighthouseClient client = new LighthouseClient(cred.Get(), Region.Get(), profile.Get());
            RebootInstancesRequest req = new RebootInstancesRequest();
            req.InstanceIds = new string[] { InstanceId };
            RebootInstancesResponse resp = client.RebootInstancesSync(req);

            return !string.IsNullOrEmpty(resp.RequestId);
        }
        public bool Start()
        {
            LighthouseClient client = new LighthouseClient(cred.Get(), Region.Get(), profile.Get());
            StartInstancesRequest req = new StartInstancesRequest();
            req.InstanceIds = new string[] { InstanceId };
            StartInstancesResponse resp = client.StartInstancesSync(req);

            return !string.IsNullOrEmpty(resp.RequestId);
        }
        public bool Shutdown()
        {
            LighthouseClient client = new LighthouseClient(cred.Get(), Region.Get(), profile.Get());
            StopInstancesRequest req = new StopInstancesRequest();
            req.InstanceIds = new string[] { InstanceId };
            StopInstancesResponse resp = client.StopInstancesSync(req);

            return !string.IsNullOrEmpty(resp.RequestId);
        }
    }
}
